package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-30 18:34
 **/
public class LeetCode17 {

    List<String> list = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) {
            return list;
        }
        Map<Character, char[]> map = new HashMap<>();
        map.put('2', new char[]{'a', 'b', 'c'});
        map.put('3', new char[]{'d', 'e', 'f'});
        map.put('4', new char[]{'g', 'h', 'i'});
        map.put('5', new char[]{'j', 'k', 'l'});
        map.put('6', new char[]{'m', 'n', 'o'});
        map.put('7', new char[]{'p', 'q', 'r', 's'});
        map.put('8', new char[]{'t', 'u', 'v'});
        map.put('9', new char[]{'w', 'x', 'y', 'z'});
        recur(map, digits, 0, new StringBuilder());
        return list;
    }

    void recur(Map<Character, char[]> map, String digits, int index, StringBuilder builder) {
        if (index == digits.length()) {
            list.add(builder.toString());
        } else {
            char[] chars = map.get(digits.charAt(index));
            for (char aChar : chars) {
                builder.append(aChar);
                recur(map, digits, index + 1, builder);
                builder.deleteCharAt(index);
            }
        }
    }

}
